/**
 * 
 */
package com.gr.staffpm.pages.behaviour;

import org.apache.wicket.Component;
import org.apache.wicket.behavior.AbstractBehavior;
import org.apache.wicket.markup.ComponentTag;


/**
 * @author Graham Rhodes 3 Jan 2011 20:52:43
 */
public class SubmitOnEnterKeyBehaviour extends AbstractBehavior {

    private static final long serialVersionUID = 1L;
    private final Component form;

    public SubmitOnEnterKeyBehaviour(Component formToSubmit) {
        this.form = formToSubmit;
    }

    @Override
    public void bind(Component component) {
        component.setOutputMarkupId(true);
    }

    /* (non-Javadoc)
     * @see org.apache.wicket.behavior.AbstractBehavior#onComponentTag(org.apache.wicket.Component, org.apache.wicket.markup.ComponentTag)
     */
    @Override
    public void onComponentTag(Component component, ComponentTag tag) {
        super.onComponentTag(component, tag);
        tag.put("onkeypress", "if (event.keyCode == 13) document.forms['" + form.getMarkupId() + "'].submit();");
    }

    @Override
    public boolean isTemporary() {
        return false;
    }
}
